//
//  SELoginViewController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/12.
//

import UIKit

class SELoginViewController: SEBaseViewController {

    var loginSuccessHandler: ((Bool) -> Void)?

    var viewModel = SEAuthViewModel()

    @IBOutlet weak var phoneTextView: SEAuthTextFildView! {
        didSet {
            phoneTextView.viewType = .account
            phoneTextView.textField.keyboardType = .numberPad
            phoneTextView.textField.text = UserDefaults.userAccount
        }
    }
    @IBOutlet weak var codeTextView: SEAuthTextFildView! {
        didSet {
            codeTextView.viewType = .code
            codeTextView.delegate = self
        }
    }
    @IBOutlet weak var loginBtnView: SEConfirmBtnView! {
        didSet {
            loginBtnView.title = "登录"
            loginBtnView.btnBgColor = UIColor.init(hex: "03C691")
            loginBtnView.delegate = self
        }
    }

    @IBAction func agreeBtnAction(_ sender: UIButton) {
        sender.isSelected.toggle()
        viewModel.isAgree = sender.isSelected
    }

    @IBOutlet weak var rememberBtn: UIButton! {
        didSet {
            rememberBtn.isSelected = UserDefaults.remberAccountAndPsd
        }
    }

    @IBAction func rememberBtnAction(_ sender: UIButton) {
        sender.isSelected.toggle()
        UserDefaults.remberAccountAndPsd = sender.isSelected
    }

    @IBAction func showInfoBtnAction(_ sender: UIButton) {
        showAgreementView()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        addEndEditingTap()
    }

    override func setupUI() {
        super.setupUI()
        viewModel.updateData = { status in
            switch status {
            case .success:
                AppDelegate.setupXylink()
                if self.loginSuccessHandler == nil {
                    UIApplication.shared.keyWindow?.rootViewController = SETabBarController()
                } else {
                    self.dismiss(animated: true) {
                        self.loginSuccessHandler?(true)
                    }
                }
            case .auth(let msg):
                SEHUD.showError(message: msg)
                self.loginSuccessHandler?(false)
            case .failure(let msg):
                SEHUD.showError(message: msg)
                self.loginSuccessHandler?(false)
            }
        }

        viewModel.checkRegisterHandler = { isRegister, status in
            switch status {
            case .success:
                if isRegister {
                    self.login()
                } else {
                    self.pushToRegister()
                }
            case .auth(let msg), .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }

        viewModel.msgCodeHandler = { status in
            switch status {
            case .success:
                self.codeTextView.fireTimer()
                SEHUD.show(message: "验证码发送成功")
            case .auth(let msg), .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    @objc private func rightItemAction() {
        pushToRegister()
    }

    private func pushToRegister() {
        let vc = SEUserInfoViewController(authViewModel: viewModel)
        navigationController?.pushViewController(vc, animated: true)
    }

    private func showAgreementView() {
        view.endEditing(true)
        let agreementView = UINib.loadViewFrom(AgreementAlertView.self)
                        agreementView.show(type: .doctorRegister)
    }

}

extension SELoginViewController: SEConfirmBtnViewDelegate {
    func confirmBtnViewAction(view: SEConfirmBtnView) {
        self.view.endEditing(true)
        checkRegister()
    }
}

extension SELoginViewController: SEAuthTextFildViewDelegate {
    func authTextFildViewCodeBtnAction() {
        view.endEditing(true)
        msgCode()
    }
}

extension SELoginViewController {
    private func msgCode() {
        viewModel.account = phoneTextView.textField.text!
        viewModel.requestMsgCode()
    }
    private func login() {
        viewModel.requestLogin()
    }
    private func checkRegister() {
        viewModel.account = phoneTextView.textField.text!
        viewModel.code = codeTextView.textField.text!
        viewModel.requestCheckRegister()
    }
}
